package com.etrans.web.gps.business.systemProcessing.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.etrans.web.gps.business.orderReceiving.domain.Alarm;
import com.etrans.web.gps.business.systemProcessing.domain.SystemProcessing;
import com.etrans.web.gps.business.systemProcessing.service.SystemProcessingService;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.SpringWebContextUtil;
import com.mongodb.QueryOperators;

public class SystemProcessingQuartzJob implements Job{

	@Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
		SystemProcessingService systemProcessingService = (SystemProcessingService)SpringWebContextUtil.getApplicationContext().getBean("systemProcessingService");
		long start = System.currentTimeMillis();
		try {
		HashMap<String, Object> param=new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String notice_time = sdf.format(new Date());
		param.put("enable", 1);
		param.put("notice_time", notice_time);
		List<SystemProcessing> spList = systemProcessingService.querySystemProcessingByEnable(param);
		for (SystemProcessing vo : spList) {
			HashMap< String,Object> whereMap = new  HashMap<String,Object>();
			HashMap< String,Object> whereMap_alarmType = new  HashMap<String,Object>();
			if(vo.getAlarm_type()!=null){
				List<Integer> at=new ArrayList<>();
				String[] alarmTypes = vo.getAlarm_type().split(",");
				if(alarmTypes!=null && alarmTypes.length>0){
					for (String alarmType : alarmTypes) {
						at.add(Integer.parseInt(alarmType));
					}
				}
				whereMap_alarmType.put(QueryOperators.IN,at);
				whereMap.put("alarm_type",whereMap_alarmType);
			};
			HashMap< String,Object> sortMap= new HashMap<String,Object>();
			if(vo.getCreate_time()!=null){
				HashMap<String,Object> timeMap = new HashMap<String,Object>();
				if(vo.getCreate_time()!=null&&!"".equals(vo.getCreate_time())){
					timeMap.put("$gte",vo.getCreate_time());
					timeMap.put("$lte", new Date());
				}
				whereMap.put("alarm_time",timeMap);
				sortMap.put("alarm_time",1);
			}
			List<Alarm> alarmList = MGTemplete.find("egov", "alarm_result", whereMap,sortMap,Alarm.class,false);
			HashMap<String,Object>  spMap = new HashMap<String,Object>();
			for (Alarm alarm : alarmList) {
				spMap.put("vehicle_no", alarm.getVehicle_no());
				spMap.put("vehicle_color", alarm.getVehicle_color());
				spMap.put("alarm_area", alarm.getArea_id());
				spMap.put("alarm_time", DateUtil.getDateTimeString(alarm.getAlarm_time(), null));
				//过滤已经处理的数据     
				Integer handleCount = systemProcessingService.querySystemProcessingCount(spMap);
				if(handleCount<1){
					List<HashMap<String, Object>> alarmTypes = systemProcessingService.queryAllAlarmType();
					for (HashMap<String, Object> map : alarmTypes) {
						String id =  map.get("id").toString();
						if(id.equals(alarm.getAlarm_type()+"")){
							String msg="贵公司车牌【"+alarm.getVehicle_no()+"】于【"+DateUtil.getDateTimeString(alarm.getAlarm_time(), null)+"】提示【"+((String) map.get("text")).replace("报警", "")+"】报警；请核查，谢谢";
							vo.setHandle_remark(msg);
						}
					} 
					String tips =systemProcessingService.sendInform(alarm,vo);
					Map<String,Object>  tipMap = new HashMap<String,Object>();
					if("success".equals(tips)){
						//保存数据     
						systemProcessingService.saveDetail(alarm,vo);
					}else{
						tipMap.put("tips",tips);
					}
				}
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("=============自动处理，用时：" + (end-start)/1000 + "." + (end-start)%1000 + "秒==============");
    }
}
